package com.yanqu.road.dao.impl.activity.dafuweng;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.activity.dafuweng.DFWEvent;
import com.yanqu.road.entity.enums.activity.dafuweng.eDaFuWengEventType;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class DFWEventConfigDaoImpl extends TempDao {

    private static final String sql = "select * from t_s_monopoly_event where activityId = ?";

    public Map<Integer, DFWEvent> getDFWEventMap(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, DFWEvent> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setInt(1, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                DFWEvent config = new DFWEvent();
                config.setEventId(rs.getInt("id"));
                config.setEventType(eDaFuWengEventType.getTypeByValue(rs.getInt("type")));
                config.setEventName(rs.getString("name"));
                config.setDesc(rs.getString("desc"));
                config.setBubble(rs.getString("bubble"));
                config.setAppearance(rs.getString("appearance"));
                config.setRewards(rs.getString("rewards"));
                config.setParamList(StringUtils.stringToIntegerList(rs.getString("paramList"), "\\|"));
                config.setUpgradeParamList(StringUtils.stringToIntegerList(rs.getString("upgradeParamList"), "\\|"));
                config.setSound(rs.getString("sound"));
                config.setDubbing(rs.getString("dubbing"));
                map.put(config.getEventId(), config);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

}
